home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- Copyright (c) 2008 Ensolis, LLC. All Rights Reserved.
- ----------------------------------------------------------------------------*/
-
- /******************************************************************************
- * Get the url to the radar cache.
- *
- * @param The name of the radar file.
- * @return The url to the cache file.
- ******************************************************************************/
- function getRadarURL(aName)
- {
- //get the disk service
- var mgrSvc = Cc["@ensolis.com/forecastfox/manager-service;1"].
- getService(Ci.ffIManagerService);
- var dskSvc = mgrSvc.disk;
-
- //get file
- var file = dskSvc.get(aName, TYPE_CACHE);
- if (!file.exists())
- return "";
-
- //convert the file to a URL
- return dskSvc.getFileURL(file);
- }
-
- /******************************************************************************
- * Interface for describing an icon pack. The component that
- * implements this interface should not be called directly, but instead
- * gotten from the icon service interface.
- *
- * @status FROZEN
- * @version 1.0
- ******************************************************************************/
- function PackItem()
- {
- this._properties = {};
- this._items = {};
- }
- PackItem.prototype = {
- __proto__: new ItemBase("PackItem"),
- _items: null,
-
- ////////////////////////////////
- // ffIPackItem
-
- /**
- * Name of the icon pack.
- */
- get name() { return this.getProperty("name"); },
-
- /**
- * Icon pack has a specific item.
- *
- * @param ID of the icon item.
- * @return True if item exists.
- */
- hasItem: function PackItem_hasItem(aID)
- {
- return this._items.hasOwnProperty(aID);
- },
-
- /**
- * Retrieve a an icon item. Method first tries to find the icon
- * in the list of items. If it doesn't exist it creates one from the
- * pack properties.
- *
- * @param ID of the icon item.
- * @return An ffIIconItem.
- */
- getItem: function PackItem_getItem(aID)
- {
- //setup variables
- var item = null;
- var unique = aID.split("-");
- var index = unique[0];
- var size = unique[1];
-
- //return radar item
- if (size == "large" && index == "radar")
- item = this._getRadarItem();
-
- //return a stored item
- else if (this.hasItem(aID))
- item = this._items[aID];
-
- //create the item from the pack info
- else {
- item = Cc["@ensolis.com/forecastfox/icon-item;1"].
- createInstance(Ci.ffIIconItem);
-
- //get the icon image info from the pack
- item.setProperty("width", this.getProperty(size + "Width"));
- item.setProperty("height", this.getProperty(size + "Height"));
- item.setProperty("size", size);
- item.setProperty("index", index);
- item.setProperty("isExemption", false);
- }
-
- //set the url
- if (!item.hasProperty("URL"))
- item.setProperty("URL", this._getURL(item.index, size));
-
-
- //save the item for later use
- this.setItem(item);
-
- //return the item
- return item;
- },
-
- /**
- * Retrieve an array of icon items.
- *
- * @param Only return the exemptions.
- * @param Count of items in the array.
- * @return An array of ffIIconItem components.
- */
- getItems: function PackItem_getItems(aExemption, aCount)
- {
- //loop through items and add to array
- var items = [];
- for (var id in this._items) {
-
- //save item if we want all or we only want exemption and it is
- if (!aExemption || (aExemption && this._items[id].isExemption))
- items.push(this._items[id]);
- }
-
- //sort the array
- items.sort();
-
- //return the array
- aCount.value = items.length;
- return items;
- },
-
- /**
- * Sets an icon item. Used for the exemptions list.
- *
- * @param The icon item.
- */
- setItem: function PackItem_setItem(aItem)
- {
- this._items[aItem.ID] = aItem.clone();
- },
-
- /**
- * Remove an icon item.
- *
- * @param ID of the icon item.
- */
- deleteItem: function PackItem_deleteItem(aID)
- {
- //do nothing if item not set
- if (!this.hasItem(aID))
- return;
-
- //delete the item
- delete this._items[aID];
- },
-
- /**
- * Retrieves the URL of the preview image. Returns an empty string if
- * preview not set.
- */
- getPreviewURL: function PackItem_getPreviewURL()
- {
- //jar file not setup
- if (!this.hasProperty("jar"))
- return "";
-
- //get the disk service
- var mgrSvc = Cc["@ensolis.com/forecastfox/manager-service;1"].
- getService(Ci.ffIManagerService);
- var dskSvc = mgrSvc.disk;
-
- //get the jar file
- var file = dskSvc.get(this.getProperty("jar"), TYPE_ICONS);
-
- //open file in zip reader
- var reader = Cc["@mozilla.org/libjar/zip-reader;1"].
- createInstance(Ci.nsIZipReader);
-
- //toolkit 2.0 and prior or non toolkit
- if ("init" in reader) {
- reader.init(file);
- reader.open();
-
- //toolkit 3.0
- } else
- reader.open(file);
-
- //try to get the preview entry
- var entry = null;
- try {
- entry = reader.getEntry("preview.png");
-
- //close the reader
- } finally {
- reader.close();
- }
-
- //return empty string if preview didn't exist
- if (!entry)
- return "";
-
- //return url
- file = dskSvc.get(file.leafName + "!", TYPE_ICONS);
- file.append("preview.png");
- return "jar:" + dskSvc.getFileURL(file);
- },
-
- /**
- * Make a duplicate copy of an icon pack.
- *
- * @return A ffIPackItem with the same values as the current item.
- */
- clone: function PackItem_clone()
- {
- //create a new pack item
- var item = Cc["@ensolis.com/forecastfox/pack-item;1"].
- createInstance(Ci.ffIPackItem);
-
- //loop through all properties and set on new pack
- for (var name in this._properties)
- item.setProperty(name, this._properties[name]);
-
- //loop through items
- for (var id in this._items)
- item.setItem(this._items[id]);
-
- //return the new pack
- return item;
- },
-
- ////////////////////////////////
- // Internal Functions
-
- /**
- * Retrieve the url the icon item.
- *
- * @param Index of the item.
- * @param The size text
- * @return URL for the item.
- */
- _getURL: function PackItem__getURL(aIndex, aSize)
- {
- //index not found
- if (aIndex == "N/A" || aIndex == "")
- return "";
-
- //jar file not setup
- if (!this.hasProperty("jar"))
- return "";
-
- //get the disk service
- var mgrSvc = Cc["@ensolis.com/forecastfox/manager-service;1"].
- getService(Ci.ffIManagerService);
- var dskSvc = mgrSvc.disk;
-
- //get the nsIFile of the image
- var file = dskSvc.get(this.getProperty("jar") + "!", TYPE_ICONS);
- file.append(aSize);
- file.append(aIndex + ".png");
-
- //then convert it to a URL
- return "jar:" + dskSvc.getFileURL(file);
- },
-
- /**
- * Get the radar icon item. Check if we already have the item and the
- * file name is the same. Otherwise create from scratch and cache the
- * item for later use.
- *
- * @return A ffIIconItem representing a large radar icon.
- */
- _getRadarItem: function PackItem__getRadarItem()
- {
- //get the radar name
- var name = "radar-" + getPref("profile.current") + ".gif";
-
- //we do not have a radar icon stored so create it.
- var item = null;
- if (!this.hasItem("radar-large")) {
-
- //create a item
- item = Cc["@ensolis.com/forecastfox/icon-item;1"].
- createInstance(Ci.ffIIconItem);
-
- item.setProperty("URL", getRadarURL(name));
- item.setProperty("size", "large");
- item.setProperty("index", "radar");
- item.setProperty("isExemption", false);
- item.setProperty("file", name);
- item.setProperty("width", 0);
- item.setProperty("height", 0);
-
- //we do have one stored so see if it is the same
- } else {
- item = this._items["radar-large"];
-
- //the radar is not the same. Delete the item and call ourself again
- if (item.getProperty("file") != name) {
- this.deleteItem("radar-large");
- item = this._getRadarItem();
- return item;
- }
- }
-
- //return the item
- return item;
- }
- };